﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using dokuzeylulworkshop.BaseUtility;
using DokuzEylul.Sinav.Entities;
using System.Collections.Generic;
using DokuzEylul.Sinav.Utility_Master;

namespace DokuzEylul.Sinav
{
    public partial class WebForm3 : BasePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                GridView1.DataBind();
                ddlParentID.DataSource = MenuUtilities.Instance.GetParentList();
                ddlParentID.DataBind();
                ddlParentID.Items.Insert(0, new ListItem("Ana Dizin", "0"));
                this.IsSave = true;
            }
        }
        public override int m_MenuID
        {
            get
            {
                return 10101;
            }
        }
        protected void GridView1_DataBinding(object sender, EventArgs e)
        {
            GridView1.DataSource = this.CurrentMenuList;
        }
        public IList<SinavMenu> CurrentMenuList
        {
            get
            {
                IList<SinavMenu> _result = Session["Current_Menu_List"] as IList<SinavMenu>;
                if (_result == null)
                    Session["Current_Menu_List"] = _result = MenuUtilities.Instance.GetMenuList();
                return _result;
            }
            set
            {
                Session["Current_Menu_List"] = value;
            }
        }
        public SinavMenu CurrentMenu
        {
            get
            {
                SinavMenu _result = Session["Current_Menu"] as SinavMenu;
                if (_result == null)
                    Session["Current_Menu"] = _result = new SinavMenu();
                return _result;
            }
            set
            {
                Session["Current_Menu"] = value;
            }
        }
        public bool IsSave
        {
            get
            {
                return (bool)ViewState["IsSave"];
            }
            set
            {
                ViewState["IsSave"] = value;
            }
        }

        protected void btnKaydet_Click(object sender, EventArgs e)
        {
            this.CurrentMenu.MenuParentId = int.Parse(ddlParentID.SelectedValue);
            this.CurrentMenu.MenuTitle = txtMenuTitle.Text;
            this.CurrentMenu.MenuType = int.Parse(ddlMenuType.SelectedValue);
            this.CurrentMenu.MenuUrl = txtUrl.Text;
            if (IsSave)
            {
                this.CurrentMenu.MenuId = int.Parse(txtMenuId.Text);
                m_dbmaster.dbfactory.Save(CurrentMenu);
                this.CurrentMenuList.Add(CurrentMenu);
                this.clearform();
            }
            else
            {
                m_dbmaster.dbfactory.Update(CurrentMenu);
                for (int i = 0; i < CurrentMenuList.Count; i++)
                {
                    if (CurrentMenu.MenuId == CurrentMenuList[i].MenuId)
                    {
                        CurrentMenuList[i].MenuId = CurrentMenu.MenuId;
                        CurrentMenuList[i].MenuParentId = CurrentMenu.MenuParentId;
                        CurrentMenuList[i].MenuTitle = CurrentMenu.MenuTitle;
                        CurrentMenuList[i].MenuType = CurrentMenu.MenuType;
                        CurrentMenuList[i].MenuUrl = CurrentMenu.MenuUrl;
                    }
                }
            }
            GridView1.DataBind();
        }
        protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
        {
            GridViewRow row = GridView1.SelectedRow;
            this.CurrentMenu = MenuUtilities.Instance.GetMenu(int.Parse(row.Cells[1].Text));
            txtMenuId.Text = this.CurrentMenu.MenuId.ToString();
            txtMenuTitle.Text = this.CurrentMenu.MenuTitle;
            txtUrl.Text = this.CurrentMenu.MenuUrl;
            ddlMenuType.SelectedValue = this.CurrentMenu.MenuType.ToString();
            ddlParentID.SelectedValue = this.CurrentMenu.MenuParentId.ToString();

            txtMenuId.Enabled = false;
            pnlcontent.Enabled = true;
            this.IsSave = false;
        }
        protected void btnYeni_Click(object sender, EventArgs e)
        {
            this.clearform();
            txtMenuId.Enabled = true;
            pnlcontent.Enabled = true;
            this.IsSave = true;
        }
        protected void btnSil_Click(object sender, EventArgs e)
        {
            CurrentMenuList.Remove(CurrentMenu);
            MenuUtilities.Instance.DeleteMenu(CurrentMenu);
            this.clearform();
        }
        public void clearform()
        {
            txtMenuId.Text = "";
            txtMenuTitle.Text = "";
            txtMenuTitle.Text = "";
            txtUrl.Text = "";
            ddlMenuType.SelectedIndex = 0;
            ddlMenuType.SelectedIndex = 0;

        }
    }


}

